<template>
    <div name="leaveManager">
       
        <el-table size="large" v-loading="pager.loading" :data="pager.lists" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" :selectable="selectInit" />
            <el-table-column label="姓名" prop="realname" />
            <el-table-column label="部门" prop="deptname" />
            <el-table-column label="类型" prop="holiday_desc" />
            <el-table-column label="请假时间" prop="leave_time" />
            <el-table-column label="部门审核" prop="level1_text">
                <template #default="{ row }">
                    <el-tag class="ml-2" v-if="row.level1==-1 || row.level1==0" type="info">{{ row.level1_text }}</el-tag>
                    <el-tag class="ml-2" v-if="row.level1==10" type="success">{{ row.level1_text }}</el-tag>
                    <el-tag class="ml-2" v-if="row.level1==-10" type="warning">{{ row.level1_text }}</el-tag>
                    <el-tag class="ml-2" v-if="row.level1==-20" type="danger">{{ row.level1_text }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="分管审核" prop="level2_text" >
                <template #default="{ row }">
                    <el-tag class="ml-2" v-if="row.level2==-1 || row.level2==0" type="info">{{ row.level2_text }}</el-tag>
                    <el-tag class="ml-2" v-if="row.level2==10" type="success">{{ row.level2_text }}</el-tag>
                    <el-tag class="ml-2" v-if="row.level2==-10" type="warning">{{ row.level2_text }}</el-tag>
                    <el-tag class="ml-2" v-if="row.level2==-20" type="danger">{{ row.level2_text }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="主管审核" prop="level3_text">
                <template #default="{ row }">
                    <el-tag class="ml-2" v-if="row.level3==-1 || row.level3==0" type="info">{{ row.level3_text }}</el-tag>
                    <el-tag class="ml-2" v-if="row.level3==10" type="success">{{ row.level3_text }}</el-tag>
                    <el-tag class="ml-2" v-if="row.level3==-10" type="warning">{{ row.level3_text }}</el-tag>
                    <el-tag class="ml-2" v-if="row.level3==-20" type="danger">{{ row.level3_text }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="审批状态" prop="status_desc">
                <template #default="{ row }">
                    <el-tag class="ml-2" v-if="row.status==0" type="info">{{ row.status_desc }}</el-tag>
                    <el-tag class="ml-2" v-if="row.status==10" type="success">{{ row.status_desc }}</el-tag>
                    <el-tag class="ml-2" v-if="row.status==20" type="primary">{{ row.status_desc }}</el-tag>
                    <el-tag class="ml-2" v-if="row.status==-10" type="warning">{{ row.status_desc }}</el-tag>
                    <el-tag class="ml-2" v-if="row.status==-20" type="danger">{{ row.status_desc }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="120" fixed="right">
                <template #default="{ row }">
                    <el-button v-if="row.isapproval" type="primary" @click="handleEdit(row)" link>
                        审批
                    </el-button>
                    <el-button  type="info" link @click="handleLog(2,row)">
                        日志
                    </el-button>
                    <el-button v-if="row.status == 20" type="danger" link @click="handleDelete(row.id)">
                        销假
                    </el-button>
                </template>
            </el-table-column>

        </el-table>
        <div style="margin-top: 20px">

            <el-button @click="toggleSelection()">取消全选</el-button>
        </div>
        <div class="flex justify-end mt-4">
            <pagination v-model="pager" @change="getLists" />
        </div>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
        <log-popup v-if="showEdit" ref="logRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>
<script lang="ts" setup name="leaveManager">
import type { ElTable, FormInstance } from 'element-plus'
import request from '@/utils/request'
import useUserStore from '@/stores/modules/user'
import { usePaging } from '@/hooks/usePaging'
import { LeaveAdminLists, leaveDelete } from '@/api/leave'
import EditPopup from './appedit .vue'
import LogPopup from '../log.vue'
import { getRoutePath } from '@/router'
import feedback from '@/utils/feedback'
const userStore = useUserStore()
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const logRef = shallowRef<InstanceType<typeof LogPopup>>()
const showEdit = ref(false)
const leaveinfo = ref<any[]>([])
// const myinfo = async () => {
//     leaveinfo.value = await request.post({ url: '/approval.leave/myinfo', params: { mid: userStore.userInfo.id } })
//     console.log('leaveinfo=', leaveinfo.value);
// }
const selectInit = (row :any) => {
    let judge = true;
    if (row.status == 0 || row.status == 10) {
        judge = true;
    } else {
        judge = false;
    }
    return judge;
}
const handleSelectionChange = () => {
    //multipleSelection.value = val
}
const queryParams = reactive({
    mid: userStore.userInfo.id
})
const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: LeaveAdminLists,
    params: queryParams
})
/**查阅审批日志 */
const handleLog = async (fid:number,data: any) => {
    showEdit.value = true
    await nextTick()
    logRef.value?.open('edit')
    logRef.value?.getDetail(fid,data)
    getLists()
}
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
    getLists()
}
/**取消同步 */
const toggleSelection = () => {
    
}

/**撤回请假 */
const handleDelete = async (id: number) => {
    await feedback.confirm('确定要撤回？')
    await leaveDelete({ id })
    getLists()
}
onMounted(async () => {
    //myinfo()
    await getLists()
    //console.log('lists=', pager);

    //userStore.getUserInfo()
    // console.log('userStore=', userStore.userInfo);
    // console.log('userStore=', userStore);
    // nextTick(() => {
    //     handleExpand()
    // })
})
getLists()
</script>
